﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace DBConstruct
{
    /// <summary>
    /// 实现SQL的连接字符串控制器
    /// </summary>
    public sealed class SQLConnStringBuilder : DBConnStringBuilderFactory
    {
        private SqlConnectionStringBuilder _connBuilder;

        /// <summary>
        /// 构造函数初始化SqlConnectionStringBuilder，以类中连接字符串为参数
        /// </summary>
        public SQLConnStringBuilder()
        {
            _connBuilder = new SqlConnectionStringBuilder(_connectionString);
        }

        /// <summary>
        /// 重载构造函数，根据输入的参数初始化
        /// </summary>
        /// <param name="connectionString"></param>
        public SQLConnStringBuilder(string connectionString)
        {
            _connBuilder = new SqlConnectionStringBuilder(connectionString);
        }

        /// <summary>
        /// 私有方法，设定连接字符串
        /// </summary>
        private void SetConnectionString()
        {
            _connectionString = _connBuilder.ConnectionString;
        }

        /// <summary>
        /// 添加键值对
        /// </summary>
        /// <param name="key">关键字</param>
        /// <param name="value">值</param>
        /// <returns>trueORfalse</returns>
        public override bool AddKeyValue(string key, object value)
        {
            try
            {
                _connBuilder.Add(key, value);
                SetConnectionString();
            }
            catch (Exception ex)
            {
                throw new ArgumentNullException();
                return false;
            }
            return true;
        }

        /// <summary>
        /// 移除指定的ConnectionString中存在的键值对
        /// </summary>
        /// <param name="key">关键字</param>
        /// <returns>trueORfalse</returns>
        public override bool RemoveKeyValue(string key)
        {
            try
            {
                _connBuilder.Remove(key);
                SetConnectionString();
            }
            catch (Exception ex)
            {
                throw new ArgumentNullException();
                return false;
            }
            return true;
        }

        /// <summary>
        /// 设置ConnectionString中存在的键的值
        /// </summary>
        /// <param name="key">关键字</param>
        /// <param name="value">设定值</param>
        /// <returns>trueORfalse</returns>
        public override bool SetKeyValue(string key, object value)
        {
            if (ContainKey(key))
            {
                _connBuilder.Add(key, value);
                SetConnectionString();
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 检查是否存在某一键
        /// </summary>
        /// <param name="key">关键字</param>
        /// <returns>trueORfalse</returns>
        public override bool ContainKey(string key)
        {
            return _connBuilder.ShouldSerialize(key);
        }

        /// <summary>
        /// 移除所有键值对
        /// </summary>
        public override void ClearAll()
        {
            _connBuilder.Clear();
            SetConnectionString();
        }
    }
}
